﻿namespace Sharemarker.Service
{
    using System.ServiceModel;
    using System.ServiceProcess;

    using Autofac;

    using log4net;

    /// <summary>
    /// 
    /// </summary>
    public partial class SharemarkerService : ServiceBase
    {
        #region Static Fields

        /// <summary>
        /// 
        /// </summary>
        private static readonly ILog Logger = LogManager.GetLogger(typeof(SharemarkerService));

        #endregion

        #region Fields

        /// <summary>
        /// 
        /// </summary>
        private ServiceHost boaServiceHost;

        /// <summary>
        /// 
        /// </summary>
        private ServiceHost securityCheckServiceHost;

        /// <summary>
        /// 
        /// </summary>
        private ISharemarkerManager sharemarkerManager;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SharemarkerService" /> class.
        /// </summary>
        public SharemarkerService()
        {
            this.InitializeComponent();
        }

        #endregion

        #region Methods

        /// <summary>
        /// When implemented in a derived class, executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically). Specifies actions to take when the service starts.
        /// </summary>
        /// <param name="args">Data passed by the start command.</param>
        protected override void OnStart(string[] args)
        {
            if (this.boaServiceHost != null)
            {
                this.boaServiceHost.Close();
            }

            if (this.securityCheckServiceHost != null)
            {
                this.securityCheckServiceHost.Close();
            }

            this.sharemarkerManager = new SharemarkerManager();

            this.boaServiceHost = new ServiceHost(typeof(BOAService));
            this.boaServiceHost.Open();

            this.securityCheckServiceHost = new ServiceHost(typeof(SecurityCheckService));
            this.securityCheckServiceHost.Open();

            var containerBuilder = new ContainerBuilder();
            DependencyServiceRegister.Register(containerBuilder, new ServiceConfig());

            IContainer container = containerBuilder.Build();
            container.InjectProperties(this);
        }

        /// <summary>
        /// When implemented in a derived class, executes when a Stop command is sent to the service by the Service Control Manager (SCM). Specifies actions to take when a service stops running.
        /// </summary>
        protected override void OnStop()
        {
            if (this.boaServiceHost != null)
            {
                this.boaServiceHost.Close();
                this.boaServiceHost = null;
            }

            if (this.securityCheckServiceHost != null)
            {
                this.securityCheckServiceHost.Close();
                this.securityCheckServiceHost = null;
            }
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        private static void Main()
        {
            Run(new ServiceBase[] { new SharemarkerService() });
        }

        #endregion
    }
}